package sunsun.xiaoli.jiarebang.custom;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ViewPagerSliding extends ViewPager {
    //是否可以进行滑动
     private boolean isSlide = false;
     public void setSlide(boolean slide) {
         isSlide = slide;
     }
     public ViewPagerSliding(Context context) { super(context); }
     public ViewPagerSliding(Context context, AttributeSet attrs) { super(context, attrs); }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        /* return false;//super.onTouchEvent(arg0); */
        if (isSlide)
            return false;
        else
            return super.onTouchEvent(arg0);
    }

     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         if (isSlide)
             return false;
         else
             return super.onInterceptTouchEvent(ev);
     }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height) height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}
